package battleship;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

public class RunningGamesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("RunningGamesServlet: starting");
//        ArrayList<Game> runningGames = Context.runningGames;
//        Iterator<Game> iterator = runningGames.iterator();
//        StringBuilder sb = new StringBuilder();
//        if (iterator.hasNext()) {
//            Game game = iterator.next();
//            sb.append(game.player1.getUsername() + "," + game.player2.getUsername());
//        }
//        while (iterator.hasNext()) {
//            Game game = iterator.next();
//            User player1 = game.player1;
//            User player2 = game.player2;
//            String username1 = player1.getUsername();
//            String username2 = player2.getUsername();
//            sb.append(";" + username1 + "," + username2);
//        }
//        String usernamePairs = sb.toString();
//
//        response.setContentType("text/plain");
//        PrintWriter out = response.getWriter();
//        out.print(usernamePairs);
//        out.close();
//        System.out.println("runningGamesServlet: " + usernamePairs);
//
//
//        // temp, testing
//        HttpSession session = request.getSession();
//        LinkedBlockingQueue<Game> openGamesQueue = (LinkedBlockingQueue<Game>) session.getAttribute("openGamesQueue");
//        Game game3 = new Game();
//        User chihiro = new User();
//        chihiro.setUsername("chihiro");
//        game3.setPlayer1(chihiro);
//        Context.openGames.add(game3);
//        try {
//            openGamesQueue.put(game3);
//        } catch (InterruptedException e) {
//            System.out.println("InterruptedException: " + e.getMessage());
//        }
//
//        LinkedBlockingQueue<Game> runningGamesQueue = (LinkedBlockingQueue<Game>) session.getAttribute("runningGamesQueue");
//        Game game4 = new Game();
//        User ghibli = new User();
//        ghibli.setUsername("ghibli");
//        game4.setPlayer1(ghibli);
//        User haruhi = new User();
//        haruhi.setUsername("haruhi");
//        game4.setPlayer2(haruhi);
//        Context.runningGames.add(game4);
//        try {
//            runningGamesQueue.put(game4);
//        } catch (InterruptedException e) {
//            System.out.println("InterruptedException: " + e.getMessage());
//        }
//
//        //Context.openGames.remove(game3);
//        LinkedBlockingQueue<Game> removeOldOpenGames = (LinkedBlockingQueue<Game>) session.getAttribute("removeOpenGamesQueue");
//        try {
//            removeOldOpenGames.put(game3);
//        } catch (InterruptedException e) {
//            System.out.println("InterruptedException: " + e.getMessage());
//        }
//
//        //Context.runningGames.remove(game4);
//        LinkedBlockingQueue<Game> removeOldRunningGames = (LinkedBlockingQueue<Game>) session.getAttribute("removeRunningGamesQueue");
//        try {
//            removeOldOpenGames.put(game4);
//        } catch (InterruptedException e) {
//            System.out.println("InterruptedException: " + e.getMessage());
//        }
        System.out.println("RunningGamesServlet: finished");
    }
}
